package com.letoken.platform.pub.util;

import org.apache.commons.lang3.StringUtils;

import javax.crypto.Cipher;
import javax.xml.bind.DatatypeConverter;
import java.security.*;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * RSA 加密解密
 *
 */
public class RSAUtil {

    public static final String KEY_ALGORITHM = "RSA";
    public static final String SIGNATURE_ALGORITHM = "MD5withRSA";
    public static final String SIGNATURE_SHA1WITHRSA = "SHA1withRSA";

    public static final String PUBLIC_KEY = "RSAPublicKey";
    public static final String PRIVATE_KEY = "RSAPrivateKey";
    public static final String PUBLIC_KEY_CLIENT = "RSAPublicKeyClient";

    private static Map<String, String> KEY_MAP = new HashMap<>();

    /**
     * 生成密钥
     *
     * @return 返回公钥、私钥的键值对，键为：RSAPublicKey、RSAPrivateKey
     * @throws Exception
     */
    public static Map<String, String> generateKey() throws Exception {
        KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(KEY_ALGORITHM);
        keyPairGen.initialize(2048);

        KeyPair keyPair = keyPairGen.generateKeyPair();

        // 公钥
        RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();

        // 私钥
        RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();

        Map<String, String> keyMap = new HashMap<>(2);

        String publicKeyStr = encryptBASE64(publicKey.getEncoded());
        String privateKeyStr = encryptBASE64(privateKey.getEncoded());
        keyMap.put(PUBLIC_KEY, publicKeyStr);
        keyMap.put(PRIVATE_KEY, privateKeyStr);

        return keyMap;
    }

    /**
     * 根据公钥、私钥初始化默认秘钥，初始化后在使用公钥、私钥加密解密时可以不再传参公钥私钥
     *
     * @param publicKey  公钥
     * @param privateKey 私钥
     * @return 返回公钥、私钥的键值对，键为：RSAPublicKey、RSAPrivateKey
     * @throws Exception
     */
    public static Map<String, String> initDefaultKey(String publicKey, String privateKey) throws Exception {
        AssertUtil.notBlank(publicKey, "initDefaultKey publicKey not Blank!");
        AssertUtil.notBlank(privateKey, "initDefaultKey privateKey not Blank!");

        KEY_MAP.put(PRIVATE_KEY, privateKey);
        KEY_MAP.put(PUBLIC_KEY, publicKey);

        return KEY_MAP;
    }

    /**
     * 根据公钥字符串获取公钥对象
     *
     * @param publicKeyStr
     * @return
     * @throws Exception
     */
    public static PublicKey getPublicKey(String publicKeyStr) throws Exception {
        // 解密由base64编码的公钥
        byte[] keyBytes = decryptBASE64(publicKeyStr);

        // 构造X509EncodedKeySpec对象
        X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);

        // KEY_ALGORITHM 指定的加密算法
        KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);

        // 取公钥匙对象
        PublicKey pubKey = keyFactory.generatePublic(keySpec);
        return pubKey;
    }

    /**
     * 根据私钥字符串获取私钥对象
     *
     * @param privateKeyStr
     * @return
     * @throws Exception
     */
    public static PrivateKey getPrivateKey(String privateKeyStr) throws Exception {
        // 对密钥解密
        byte[] keyBytes = decryptBASE64(privateKeyStr);
        // 取得私钥
        PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
        KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
        PrivateKey privateKey = keyFactory.generatePrivate(pkcs8KeySpec);
        return privateKey;
    }

    /**
     * 用私钥对信息生成数字签名 MD5withRSA
     *
     * @param
     * @param privateKey
     * @return
     * @throws Exception
     */
    public static String signMD5withRSA(String content, String privateKey, String charset) throws Exception {
        return sign(content, privateKey, charset, SIGNATURE_ALGORITHM);
    }

    /**
     * 用私钥对信息生成数字签名SHA1withRSA
     *
     * @param
     * @param privateKey
     * @return
     * @throws Exception
     */
    public static String signSHA1withRSA(String content, String privateKey, String charset) throws Exception {
        return sign(content, privateKey, charset, SIGNATURE_SHA1WITHRSA);
    }

    /**
     * 用私钥对信息生成数字签名
     *
     * @param content    签名内容
     * @param privateKey 私钥
     * @param charset    编码
     * @param algorithm  算法
     * @return
     * @throws Exception
     */
    public static String sign(String content, String privateKey, String charset, String algorithm) throws Exception {
        return sign(content.getBytes(charset), privateKey, algorithm);
    }

    /**
     * 用私钥对信息生成数字签名
     *
     * @param data       签名byte数据
     * @param privateKey 私钥
     * @param algorithm  算法
     * @return
     * @throws Exception
     */
    public static String sign(byte[] data, String privateKey, String algorithm) throws Exception {
        // 取私钥匙对象
        PrivateKey priKey = getPrivateKey(privateKey);

        // 用私钥对信息生成数字签名
        Signature signature = Signature.getInstance(algorithm);
        signature.initSign(priKey);
        signature.update(data);

        return encryptBASE64(signature.sign());
    }

    /**
     * 用私钥对信息生成数字签名
     *
     * @param data       加密数据
     * @param privateKey 私钥
     * @return
     * @throws Exception
     */
    public static String signMD5withRSA(byte[] data, String privateKey) throws Exception {
        return sign(data, privateKey, SIGNATURE_ALGORITHM);
    }

    /**
     * 校验数字签名
     *
     * @param data      加密数据
     * @param publicKey 公钥
     * @param sign      数字签名
     * @return 校验成功返回true 失败返回false
     * @throws Exception
     */
    public static boolean verify(byte[] data, String publicKey, String sign) throws Exception {
        // 取公钥匙对象
        PublicKey pubKey = getPublicKey(publicKey);

        Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM);
        signature.initVerify(pubKey);
        signature.update(data);

        // 验证签名是否正常
        return signature.verify(decryptBASE64(sign));
    }

    /**
     * 解密<br>
     * 用私钥解密
     *
     * @param data
     * @param key
     * @return
     * @throws Exception
     */
    public static byte[] decryptByPrivateKey(byte[] data, String key) throws Exception {

        KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
        Key privateKey = getPrivateKey(key);

        // 对数据解密
        Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
        cipher.init(Cipher.DECRYPT_MODE, privateKey);

        return cipher.doFinal(data);

    }

    /**
     * 解密<br>
     * 用私钥解密
     *
     * @param data
     * @return
     * @throws Exception
     */
    public static byte[] decryptByPrivateKey(byte[] data) throws Exception {
        ensureInitDefaultKey();
        return decryptByPrivateKey(data, KEY_MAP.get(PRIVATE_KEY));
    }

    /**
     * 解密<br>
     * 用默认私钥解密
     *
     * @param data
     * @return
     * @throws Exception
     */
    public static String decryptByPrivateKey(String data, String key) throws Exception {
        String ret = "";
        String separator = System.getProperty("line.separator", "\r\n");
        String fm = data.replaceAll(" ", separator) + separator;

        byte[] dataFm = decryptBASE64(fm);

        byte[] decryptDataBytes = decryptByPrivateKey(dataFm, key);

        ret = new String(decryptDataBytes);

        return ret;
    }

    /**
     * 用默认私钥解密
     *
     * @param data
     * @return
     * @throws Exception
     */
    public static String decryptByPrivateKey(String data) throws Exception {
        ensureInitDefaultKey();
        return decryptByPrivateKey(data, KEY_MAP.get(PRIVATE_KEY));
    }

    /**
     * 解密<br>
     * 用公钥解密
     *
     * @param data
     * @param key
     * @return
     * @throws Exception
     */
    public static byte[] decryptByPublicKey(byte[] data, String key) throws Exception {

        KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
        Key publicKey = getPublicKey(key);

        // 对数据解密
        Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
        cipher.init(Cipher.DECRYPT_MODE, publicKey);

        return cipher.doFinal(data);

    }

    /**
     * 解密<br>
     * 用默认公钥解密
     *
     * @param data
     * @return
     * @throws Exception
     */
    public static byte[] decryptByPublicKey(byte[] data) throws Exception {
        ensureInitDefaultKey();
        return decryptByPublicKey(data, KEY_MAP.get(PUBLIC_KEY));
    }

    /**
     * 解密<br>
     * 用公钥解密
     *
     * @param data
     * @return
     * @throws Exception
     */
    public static String decryptByPublicKey(String data, String key) throws Exception {
        byte[] dateBytes = decryptBASE64(data);
        byte[] decryptBytes = decryptByPublicKey(dateBytes, key);
        String ret = new String(decryptBytes);
        return ret;
    }

    /**
     * 解密<br>
     * 用默认公钥解密
     *
     * @param data
     * @return
     */
    public static String decryptByPublicKey(String data) throws Exception {
        ensureInitDefaultKey();
        return decryptByPublicKey(data, KEY_MAP.get(PUBLIC_KEY));
    }

    /**
     * 加密<br>
     * 用公钥加密
     *
     * @param data
     * @param key
     * @return
     * @throws Exception
     */
    public static byte[] encryptByPublicKey(byte[] data, String key) throws Exception {

        KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
        Key publicKey = getPublicKey(key);

        // 对数据加密
        Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);

        return cipher.doFinal(data);

    }

    /**
     * 加密<br>
     * 用公钥加密
     *
     * @param data
     * @return
     * @throws Exception
     */
    public static byte[] encryptByPublicKey(byte[] data) throws Exception {
        ensureInitDefaultKey();
        return encryptByPublicKey(data, KEY_MAP.get(PUBLIC_KEY));
    }

    /**
     * 加密<br>
     * 用公钥加密
     *
     * @param data
     * @return
     * @throws Exception
     */
    public static String encryptByPublicKey(String data, String key) throws Exception {
        byte[] encryptBytes = encryptByPublicKey(data.getBytes(), key);
        return encryptBASE64(encryptBytes);
    }

    /**
     * 加密<br>
     * 用默认公钥加密
     *
     * @param data
     * @return
     * @throws Exception
     */
    public static String encryptByPublicKey(String data) throws Exception {
        ensureInitDefaultKey();
        return encryptByPublicKey(data, KEY_MAP.get(PUBLIC_KEY));
    }

    /**
     * 加密<br>
     * 用私钥加密
     *
     * @param data
     * @param key
     * @return
     * @throws Exception
     */
    public static byte[] encryptByPrivateKey(byte[] data, String key) throws Exception {

        KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
        Key privateKey = getPrivateKey(key);

        // 对数据加密
        Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
        cipher.init(Cipher.ENCRYPT_MODE, privateKey);

        return cipher.doFinal(data);

    }

    /**
     * 加密<br>
     * 用默认私钥加密
     *
     * @param data
     * @return
     * @throws Exception
     */
    public static byte[] encryptByPrivateKey(byte[] data) throws Exception {
        ensureInitDefaultKey();
        return encryptByPrivateKey(data, KEY_MAP.get(PRIVATE_KEY));
    }

    /**
     * 加密<br>
     * 用私钥加密
     *
     * @param data
     * @return
     * @throws Exception
     */
    public static String encryptByPrivateKey(String data, String key) throws Exception {
        byte[] encryptBytes = encryptByPrivateKey(data.getBytes(), key);
        return encryptBASE64(encryptBytes);
    }

    /**
     * 加密<br>
     * 用默认私钥加密
     *
     * @param data
     * @return
     * @throws Exception
     */
    public static String encryptByPrivateKey(String data) throws Exception {
        ensureInitDefaultKey();
        return encryptByPrivateKey(data, KEY_MAP.get(PRIVATE_KEY));
    }

    /**
     * 取得私钥
     *
     * @param keyMap
     * @return
     * @throws Exception
     */
    public static String getPrivateKey(Map<String, String> keyMap) throws Exception {
        return keyMap.get(PRIVATE_KEY);
    }

    /**
     * 取得系统默认私钥
     *
     * @param
     * @return String
     */
    public static String getPrivateKey() throws Exception {
        return KEY_MAP.get(PRIVATE_KEY);
    }

    /**
     * 取得公钥
     *
     * @param keyMap
     * @return
     * @throws Exception
     */
    public static String getPublicKey(Map<String, String> keyMap) throws Exception {
        return keyMap.get(PUBLIC_KEY);
    }

    /**
     * 取得系统默认公钥
     *
     * @param
     * @return String
     */
    public static String getPublicKey() throws Exception {
        return KEY_MAP.get(PUBLIC_KEY);
    }

    /**
     * 确保已经初始化了秘钥
     *
     * @throws IllegalArgumentException
     */
    private static void ensureInitDefaultKey() throws IllegalArgumentException {
        String publickKey = KEY_MAP.get(PUBLIC_KEY);
        String privateKey = KEY_MAP.get(PUBLIC_KEY);
        if (StringUtils.isBlank(publickKey) || StringUtils.isBlank(privateKey)) {
            throw new IllegalArgumentException("Please first initDefaultKey, eg : RSACoderUtil.initDefaultKey(publicKey, privateKey)");
        }
    }

    /**
     * BASE64解密
     *
     * @param key
     * @return
     * @throws Exception
     */
    public static byte[] decryptBASE64(String key) {
        return DatatypeConverter.parseBase64Binary(key);
    }

    /**
     * BASE64加密
     *
     * @param key
     * @return
     * @throws Exception
     */
    public static String encryptBASE64(byte[] key) {
        return DatatypeConverter.printBase64Binary(key);
    }

    public static void main(String[] args) throws Exception {
//        Map map = RSAUtil.generateKey();
//        String publicKey = getPublicKey(map);
//        System.out.println(publicKey);
//
//        String privateKey = getPrivateKey(map);
//        System.out.println(privateKey);
//        String pubkey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAtxcxKNUKw5yLz/R22bPAI33W43xECBRgAUk5UqJI8nI89FqLmLj18zY9c8FAorH0Nxi3WjSvIm0sEygl0ym/D1mm1AXTE+2cZPCk728zkPlN8V4shbkHY/iNxupnsI2CblDzQIQticJk6/Wh9v2pu9BdptXZ9O6OYdYbx6pDIC5E92kjGoLAK2F58sbTLKi0a0eb6T9ZRtQjwWgHtj6/4ApQZLZGLfc8fjvAhp0TeEQ15bFrqNh35MJobCe5f9y51dqt4ou6r8ZQVEn/LYVIEWnfwxbp2jRWEXN7t+185VFdM9m5TlQFL//e/bXgFdBLSj+p6fKAURiWINJNb+EJqwIDAQAB";
//        String prikey = "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCZ7qBC3nbZxfxoUH50z7Q6g4fx+WDK9H7ItgUUFz8T0VB3G6o/dgJ5Ns5GW8VekEmL5exhvkoDw/u5ddzxPbB23i1wIItSL8onrluQMAFKdRakSNAAmUYJs12yt+x6DVJCIbphHjiOhHLJh+pviADywg7XliVBnkcCwRe2yowq26xxeJwsKKOGIatnbxl6iWi4GI/nZdemliAxanWVG/QUZNghg0FM4aiusBjsTBJTB5J0MXEzsY1vXgYZnpVMa//vL9vxsGaK+991fZ810bdqeOv8zeri0FKKsCzi8BYFve0yhTj/0DRCC/vx3OP1wFT+51/d/Vn5of/r4Y8kLGCNAgMBAAECggEAWuI/EPwUJAP3QQe1gfZYLOwXydA5S9sLOCnv2arX/TVqUsisvgdI8pRfOb9ZSrnaEpVP+W3GeW6vIxLpvi10ENqg7UDpwT6iFselG/6b5IA5PAoZdSEJ7xRN0AUzw+Q1mwIa7thPACZHN6Hd0rf0B2B4LuVhXdq7hm7HN49jLrHUi79NH18Przx/XTh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x3CpmcNL3Ogl2YIpU2E/tzZa3Zwdye4ToP/MaHTsnWKl59z2t3OgHf2oGk/eqUH0iawjyQLL3lKUwiGjzIAOzg04XFYCQQq6mhWXk3qMUX5Eiu8B3Vxi2tah90mbo6fyPfW6Sz7RDFiZlK0f2IX7CfYWLGN6oEfH3S1b0x1ZtKQzfJyUooQike4ua4MSRWc8giaKNRK4bLj2noyplXQTJAqgNB28GrfQeKL4QkoOtkQwHeH5iTfG6eIoSWNpggGDlmEGoZCbYtqLMtzlHfXT9RjD4/rHAj7u8rJ2CXqsQOWEOLxJtUim9n/sc8anzmHf1+tA==";
////
////
////        // 公钥加密
//        String ss = encryptByPublicKey("xjIeB8", pubkey);
//        System.out.println(ss);
        // 私钥解密
//        System.out.println(decryptByPrivateKey(data, prikey));

//        String sgin = "YzNiOGQ3MDkwOTc4NGE2YTk2MjhhNmUwNmRhNjhlYTctMTYxOTA4NTMzMjMzMg==uqpH0lIBXzQ+g1MNGYEgZ5jCE+a/ZruMOova9qf63abee2K4gwjd6Fzc/GLW4KP3eEWUiCqOHnC/qLxCDDRyerj/j4bS4kCMMgt9jI4DV0U=DHmy44mvR7SzLrxY1w9mh/s9y4VSGQBk4GQwCCWAPovP/37/NE/GTB7GiZtfwaHUdHljKvh7wYlNmL/JThu1iFnrrxKgnu2rKoS3J5ozmaLrrVkk0vPZ74mJ8nSv01ptiXrCdlmY3CpbYFJZLpgOgze7g1MA4BBTI40D3+Ou58Uz68Cz5IPzOl4LejZRRyoGsUbvJbC/WZW0LDEAWbOCEe6dpD3Iwv0tbNYQ6SEFFcnvJ5QHaD0EYl6UdG4doYBkb8Tegwfk1atszoRCAOtYxzxEpTV7TNF/8sxiyp0Dqn/K726+gm6/ZcW5M8MW+iPxED999y9SZvjPCyBjVoN+mA==";
//        String c_prikey = "MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBALq/eFGA8cIOSjIexrBzTn8LuAc9n7jTKP2AKQPwmpP8S6HSLqqtTYfBYwR+2BvRyhxWKzXxQi3062yOh9xE5CW5dxSW+8yW0ryd7NcIO+LSH4nF5Z+vniOeP7fU0chF8eyMU63tSofpWPKXg8Z8IlEQkQWUW8KGjFy+kc1oDCV/AgMBAAECgYAxsrzxa9Bq5WSne/tCgB02wEakXqkABeoVtKK1srVghrcWBJp4v5Avfa69Roh6ttUDv4bWXzNOiayRgkjzvL2+O/5AO3nfQ/lH9JGD7xjJn7C+PYZ6iMqGfEy3gWYANDfL6+LpcxGUUhcqw2myWcbBZm7jDzAuoi4hGHYftGU8OQJBAN35RNtL8RR2R+SbhECOLmgN4Ul8r4RkD0FZibErduJXnvb4uZ2lMeeUheSqBhVsCrFaJlRAAeKtGjRbPOTJ33sCQQDXX91qx6tHhgK0+xFWEw+Snnr+pwaFAv0di7Ffw3lxOVZ34lq/43ZkPDbhX49p4YOMHD0lFSKj4+ioUIrPXZDNAkBTVI/H79N6RgkpRkS442eCgqtvphJSFiov80ggrdt8jz5LIwazqt94O6akODv+cMf22owHFtxeV3O98f6DOVHfAkBa33QJDd5S+1RBEH4sFzbG4l96+vZhQPWTddGNWmjvQQYe5z7rIApgGJf/V9anEiPzBic2ckh/ZjZQbktjW1MxAkBmSivjDmN6Fs/nglfG050CEe8r3R8cQ65AJ5pdqJd9LnMCf3UdxZCOb8IbCX1tFz4TuY+vfIod7MHea8xr43UZ";
////        // 验签
//        System.out.println(signMD5withRSA(sgin,c_prikey,"UTF-8"));
//
//
//
//        String content= "uqpH0lIBXzQ+g1MNGYEgZ5jCE+a/ZruMOova9qf63abee2K4gwjd6Fzc/GLW4KP32y9WSaoMz1konTE1uBtiCCLAFIz+bjxtv4B3VkDPddA=";
//
//
//
//        String prikey2="MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBALq/eFGA8cIOSjIexrBzTn8LuAc9n7jTKP2AKQPwmpP8S6HSLqqtTYfBYwR+2BvRyhxWKzXxQi3062yOh9xE5CW5dxSW+8yW0ryd7NcIO+LSH4nF5Z+vniOeP7fU0chF8eyMU63tSofpWPKXg8Z8IlEQkQWUW8KGjFy+kc1oDCV/AgMBAAECgYAxsrzxa9Bq5WSne/tCgB02wEakXqkABeoVtKK1srVghrcWBJp4v5Avfa69Roh6ttUDv4bWXzNOiayRgkjzvL2+O/5AO3nfQ/lH9JGD7xjJn7C+PYZ6iMqGfEy3gWYANDfL6+LpcxGUUhcqw2myWcbBZm7jDzAuoi4hGHYftGU8OQJBAN35RNtL8RR2R+SbhECOLmgN4Ul8r4RkD0FZibErduJXnvb4uZ2lMeeUheSqBhVsCrFaJlRAAeKtGjRbPOTJ33sCQQDXX91qx6tHhgK0+xFWEw+Snnr+pwaFAv0di7Ffw3lxOVZ34lq/43ZkPDbhX49p4YOMHD0lFSKj4+ioUIrPXZDNAkBTVI/H79N6RgkpRkS442eCgqtvphJSFiov80ggrdt8jz5LIwazqt94O6akODv+cMf22owHFtxeV3O98f6DOVHfAkBa33QJDd5S+1RBEH4sFzbG4l96+vZhQPWTddGNWmjvQQYe5z7rIApgGJf/V9anEiPzBic2ckh/ZjZQbktjW1MxAkBmSivjDmN6Fs/nglfG050CEe8r3R8cQ65AJ5pdqJd9LnMCf3UdxZCOb8IbCX1tFz4TuY+vfIod7MHea8xr43UZ";
//        String data2="aaqZ67LTpIL8dGKbZJ5PceMqib36T1krJ/+7PSGU63ogOodbdR8NcPSa25XxRU8zSWruCackyhZ3JhVubhGQrZ5ZNSVGjrLUF41rh3CMPreLM4iYifN5gDwJOTVPdVpcqXUagtmjsr+fyjlGeER7FrDv0eutbgieVbD6h4ozSdU=";
//        System.out.println(decryptByPrivateKey(data2, prikey2));







//        encryptByPublicKey("biN/+I93Y48+L974JnUDKv5uuoAlUhl3AyGF8kRw2VAoiyzLo7LxanhjXclAq/o/L10CMzmlV2oLyNH5OOkAYFPqtIl6tfb5RtZHVei+lLg0bnbxGvgOJCMYSA+0xOpKH6sE2sZU/HJ4qNK5VxMpARoccxmEbJawuAiNZBr48K9hMyNUc5pTw391A9czciaYvJ0OLrP+gv4ZTUYqNQ0xjGPeAxCVwc9FEvmamuPeZo5g6IoMQUUXcmlLpHuzDcsNuwNSuTYjSCO5BCJn+HLhFeOz2lhLxCvMNbQCAEg0QX+WXbMtQX1uzA+v7NB8XruGhaz+zfy2mKf89/Ma0xXjfA==.Y3lxvyxFzIUAiPq7l8QkPHmAp50jQpFP/UBR9jchTQVBbaPVPvdlGrNsqhJVJVhY5RZUx6h3B9R8UExJIURvaTyOLXvc8s3OduuMpOz794uXcmfwpma3KwRrbA0HpXm2B5xDNXJQWflzIuRCL0ol57794MyUAc3CcsSJIiKjttF3Xtof3X0d93Fa6e2JRCC0zVlGW5nkYstmaDwGroiXZMbYisuuuxIOVJrY7K2/SOiHPZbPd1iYxDjG8cISUTy4Bya4fbKHTMAoNasUV1UrVSczcXjCO97lmE52lNrVNOjN4hZsO0LsSny08MpbBfL03C3UqgDa67pPKa1cNNJFfw==","MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAhfjDjC5YH3xySBGXdwaEOCYUUDiMOZJAuDmopkDbaxVqhssPF2XojUGnTwnAklvh4uUTlmquHWt0N/NFdplkogruwuuKcEkLg+ppzHlZHB4qj+UpvjMWetIsaXDtSFr4hUmF2uQAJjFERsBQz/WadFoutPQG0K6dBuDBm5rtcIlvK18QbcCSGESEO1d6VroYHNQAgfa3CM/PCeOFlU8IkQOB1uvxWvEYTgsR1lwSkCce4kvro225r0gMrnQeZ3FFz2glNOTH81lS0mOk30ZJDOrr1yGV+TngFakT1XmqBLDTTjusVpOUSl8u1J4QM8MODYMVy7REz2yQGrSfbjm+PwIDAQAB");
//        Map<String, String> map = RSAUtil.generateKey();
//        String publicKey = getPublicKey(map);
//        System.out.println(publicKey);
//        String privateKey = getPrivateKey(map);
//        System.out.println(privateKey);
//
//        Duration duration = Duration.between( a,b);
//
//        System.out.println(duration.getSeconds());
////
        String data = "{\"date\":\"2024-07-18\",\"videoId\":13}";
////
//        // 公钥加密
        String encrypt = encryptByPublicKey(data, "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAwga+eetdKyN65Q/iuLJRYEuXiDJ35giS9Pcn/xSOPwTUugUe/dySrdfQcHTzmHhr4swr/gqCdjc13iEYQFivCFKSsazlLeZ3SoBFmozEQuwVKNe2nu4Iqq8Geyltz8Nvk4gKhFoOkj21/SivmFuG1AaihTokM7A/HRxtVEP0ExYrf368IuV8wbFh5uLKIeX1UpceK8D5glwwFNgWaSJWkaoUICcBWRAlqHpvAcgQn6A/JDH4cawJ0wnvGk90m8Hyfk0jTUwVCBI9DbXlDx/CGxQ4wmZnKY+w2MWcxRrA+H3H/jnz8bpwxUqv8E+Zmp4F3PIoDOIC+5plcG6Uig25LQIDAQAB");
        System.out.println(encrypt);
//        // 私钥解密
        System.out.println(decryptByPrivateKey(encrypt, "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDCBr55610rI3rlD+K4slFgS5eIMnfmCJL09yf/FI4/BNS6BR793JKt19BwdPOYeGvizCv+CoJ2NzXeIRhAWK8IUpKxrOUt5ndKgEWajMRC7BUo17ae7giqrwZ7KW3Pw2+TiAqEWg6SPbX9KK+YW4bUBqKFOiQzsD8dHG1UQ/QTFit/frwi5XzBsWHm4soh5fVSlx4rwPmCXDAU2BZpIlaRqhQgJwFZECWoem8ByBCfoD8kMfhxrAnTCe8aT3SbwfJ+TSNNTBUIEj0NteUPH8IbFDjCZmcpj7DYxZzFGsD4fcf+OfPxunDFSq/wT5mangXc8igM4gL7mmVwbpSKDbktAgMBAAECggEAAsqDi2/wEWqLfkeN+J+yDXtQ5Pfq4x9887Kpe+EhEbWBVOK1eSvm3ULVQATiqy8SSDstJ9ShX094gidCOMy9ydYcP5uP2WrvDN6PAcPkH91TCZkeyqb8b24gnv1v07UJQhtLIW/FNWPXXhqrefrZE+w7gsSdGDRBGCtcREHM/UhGMjkJTFW2iJhp/yAaw1snwUz96bMF/TO65tw02A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ZFwhr04eBKTOFNMNgjyB434kytNICb8w+kWNDAqdu6bsz+yu6hIk25cejtP8FUAu/Hgk17jCMtArnq5WxeJP7xvk458KUkIlHXi6gRBep36zkz0gGacr8oIITAkIZvnzQFO8AhUnQd+tUm7wFxrBDCwiTrc99SBi0GG1Qw0tbWdQF9WtOddhobBuD/r9D6rgjfcu+Ho6iFh7Q8kDFA8FiBLjFAwhuoXV5AyJmMyMHeABs6q7BOC4UQ/uw6eamJysiiAt9Ehp8kI1cXreRbi7bFIMDju+FMM/doMvzQsgzwVSTbidPoeaQ3EgB4oBc5C+nx0RrrifF41n+QIDAQAB");
//        System.out.println(a);
//
//
//        System.out.println(encryptByPrivateKey(a,"MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQC8UnqVfXtkXCGvTh4EpM4U0w2CPIHjfiTK00gJvzD6RY0MCp27puzP7K7qEiTblx6O0/wVQC78eCTXuMIy0CuerlbF4k/vG+TjnwpSQiUdeLqBEF6nfrOTPSAZpyvygghMCQhm+fNAU7wCFSdB361SbvAXGsEMLCJOtz31IGLQYbVDDS1tZ1AX1a0512GhsG4P+v0PquCN9y74ejqIWHtDyQMUDwWIEuMUDCG6hdXkDImYzIwd4AGzqrsE4LhRD+7Dp5qYnKyKIC30SGnyQjVxet5FuLtsUgwOO74Uwz92gy/NCyDPBVJNuJ0+h5pDcSAHigFzkL6fHRGuuJ8XjWf5AgMBAAECggEABHpLs97GM+kV9Y2FAxEfB/VTphnDijLv5uIog++dCqVKT0KWKP9oRYfCwXj2TYYSeFVSR6LOYZkPlRD6WviLF16p0+wFgKsmDjpnUEnsu/6GelJU67GNAJYJe593agoxdvopEnNXyoXEO9XGyqYMEaCAUOxSD9KQjbse/iV8uEIN5e9QzirxhwqU57SSOFYeHlWQbezEGtiJzsFmve4gxtOM4W1lw+NixNyAdvNs5uPj9KAmzKyQXQUalw7uWU+RbChkDUiMIlCcVIbvjoiDs8mZ6t6FxZ2MvmqfnW3NLLWCX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